package com.neusoft.data.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.neusoft.data.domain.SerChatHistory;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.data.mapper.SerCourseMapper;
import com.neusoft.data.domain.SerCourse;
import com.neusoft.data.service.ISerCourseService;

/**
 * 推荐课程Service业务层处理
 * 
 * @author slh
 * @date 2023-12-29
 */
@Service
public class SerCourseServiceImpl implements ISerCourseService 
{
    @Autowired
    private SerCourseMapper serCourseMapper;

    /**
     * 查询推荐课程
     * 
     * @param courseId 推荐课程主键
     * @return 推荐课程
     */
    @Override
    public SerCourse selectSerCourseByCourseId(Long courseId)
    {
        return serCourseMapper.selectSerCourseByCourseId(courseId);
    }

    /**
     * 查询推荐课程列表
     * 
     * @param serCourse 推荐课程
     * @return 推荐课程
     */
    @Override
    public List<SerCourse> selectSerCourseList(SerCourse serCourse)
    {
        return serCourseMapper.selectSerCourseList(serCourse);
    }

    /**
     * 新增推荐课程
     * 
     * @param serCourse 推荐课程
     * @return 结果
     */
    @Override
    public int insertSerCourse(SerCourse serCourse)
    {
        serCourse.setCreateTime(DateUtils.getNowDate());
        return serCourseMapper.insertSerCourse(serCourse);
    }

    /**
     * 修改推荐课程
     * 
     * @param serCourse 推荐课程
     * @return 结果
     */
    @Override
    public int updateSerCourse(SerCourse serCourse)
    {
        serCourse.setUpdateTime(DateUtils.getNowDate());
        return serCourseMapper.updateSerCourse(serCourse);
    }

    /**
     * 批量删除推荐课程
     * 
     * @param courseIds 需要删除的推荐课程主键
     * @return 结果
     */
    @Override
    public int deleteSerCourseByCourseIds(Long[] courseIds)
    {
        return serCourseMapper.deleteSerCourseByCourseIds(courseIds);
    }

    /**
     * 删除推荐课程信息
     * 
     * @param courseId 推荐课程主键
     * @return 结果
     */
    @Override
    public int deleteSerCourseByCourseId(Long courseId)
    {
        return serCourseMapper.deleteSerCourseByCourseId(courseId);
    }

    @Override
    public String importUser(List<SerCourse> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户不存在");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (SerCourse course : list) {
            try {
                SerCourse c = serCourseMapper.selectSerCourseByCourseId(course.getCourseId());

                if (StringUtils.isNull(c)) {
                    if (course.getCourseDesc() == null) course.setCourseDesc("null");
                    serCourseMapper.insertSerCourse(course);
                    successNum ++;
                    successMsg.append("<br/>" + successNum + "、课程 " + course.getCourseName() + " 导入成功：");
                } else if (updateSupport) {
                    serCourseMapper.updateSerCourse(course);
                    successMsg.append("<br/>" + successNum + "、课程 " + course.getCourseName() + " 更新成功：");
                } else {
                    failureNum ++;
                    failureMsg.append("<br/>" + failureNum + "、课程 " + course.getCourseName() + " 已存在：");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、课程 " + course.getCourseName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下");
            throw  new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功共 " + successNum + " 条数据");
        }

        return successMsg.toString();
    }

    @Override
    public List<String> selectSerCourseType() {

        List<SerCourse> list = serCourseMapper.selectSerCourseList(new SerCourse());
        List<String> type = list.stream()
                .map(SerCourse::getCourseName)
                .distinct()
                .collect(Collectors.toList());

        return type;
    }
}
